{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Knn的手写代码实现"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "# 样本特征\n",
    "dataX=[[1.3,6]\n",
    "       ,[3.5,5]\n",
    "       ,[4.2,2]\n",
    "       ,[5,3.3]\n",
    "       ,[2,9]\n",
    "       ,[5,7.5]\n",
    "       ,[7.2,4]\n",
    "       ,[8.1,8]\n",
    "       ,[9,2.5]]\n",
    "\n",
    "# 样本标记/分类 前4个分类为0，后5个分类为1\n",
    "dataY=[0,0,0,0,1,1,1,1,1]\n",
    "\n",
    "\n",
    "# 生成训练集：转成numpy数组\n",
    "trainX=np.array(dataX)\n",
    "trainY=np.array(dataY)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([[1.3, 6. ],\n",
       "        [3.5, 5. ],\n",
       "        [4.2, 2. ],\n",
       "        [5. , 3.3],\n",
       "        [2. , 9. ],\n",
       "        [5. , 7.5],\n",
       "        [7.2, 4. ],\n",
       "        [8.1, 8. ],\n",
       "        [9. , 2.5]]),\n",
       " array([0, 0, 0, 0, 1, 1, 1, 1, 1]))"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "trainX,trainY"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ True,  True,  True,  True, False, False, False, False, False])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "trainY==0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1.3, 3.5, 4.2, 5. ])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "trainX[trainY==0] # 取出训练集中标签为0的样本==取出前4个样本\n",
    "trainX[trainY==0,0] # 取出训练集中标签为0的样本的第0维特征==取出前4个样本的第0维特征"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAhYAAAGdCAYAAABO2DpVAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8hTgPZAAAACXBIWXMAAA9hAAAPYQGoP6dpAAAyGElEQVR4nO3deXwUZZ7H8W/nJELSCBJJJh1IhHB6oOgYQAkDIiMiLiqDB3J4rCsqx4qIx4AHBnU92PW1OLC+IhIlsqswjjMCigZBRAMSREQQEkiiYBwHuoNixyTP/tGTSJMEaHg6nePzfr3qla6qp7p+VVH6m6eernIYY4wAAAAsCAt1AQAAoPkgWAAAAGsIFgAAwBqCBQAAsIZgAQAArCFYAAAAawgWAADAGoIFAACwJqKhd1hVVaVvv/1WsbGxcjgcDb17AABwEowxKisrU2JiosLC6u+XaPBg8e2338rlcjX0bgEAgAXFxcVKSkqqd32DB4vY2FhJvsLi4uIaevcAAOAkeDweuVyums/x+jR4sKi+/BEXF0ewAACgiTneMAYGbwIAAGsIFgAAwBqCBQAAsIZgAQAArCFYAAAAawgWAADAGoIFAACwhmABAACsIVg0IGOkvDzfTwAAmqOAg0VZWZmmTJmiTp06KSYmRv369VNeXl4wamt2srOliy6SXn011JUAABAcAQeLW2+9Ve+++64WL16srVu3aujQoRoyZIi++eabYNTXbFRUSLNm+V7PmuWbBwCguQkoWBw+fFhvvPGGnnrqKV166aXq0qWLZs+erZSUFM2fPz9YNTYLS5ZIhYW+1wUFUk5OaOsBACAYAgoWFRUVqqysVKtWrfyWx8TEaN26dXVu4/V65fF4/KaWprq3ovq5LWFh9FoAAJqngIJFbGys0tPT9d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Dyy+/XFFRUfrLX/5yzHZ19Vi4XC653W7FxcWdzK4BAEAD83g8cjqdx/38PqlLIXv37tV7772nN99887hto6OjFR0dfTK7AQAATcxJXQrJyspSfHy8hg8fbrseAADQhAUcLKqqqpSVlaVx48YpIuKkx34CAIBmKOBg8d5776moqEgTJ04MRj0AAKAJC7jLYejQoTrJ8Z4AAKCZ41khAADAGoIFAACwhmABAACsIVgAAABrCBYAAMAaggUAALCGYAEAAKwhWAAAAGsIFgAAwBqCBQAAsIZgAQAArCFYAAAAawgWAADAGoIFAACwhmABAACsIVgAAABrCBYAAMAaggUAALCGYAEAAKwhWAAAAGsIFgAAwBqCBQAAsIZgAQAArCFYAAAAawgWAADAGoIFAACwhmABAACsIVgAAABrCBYAAMAaggUAALCGYAEAAKwhWAAAAGsIFgAAwBqCBQAAsIZgAQAArCFYAAAAa5p2sHC7pZKSuteVlPjWAwCABhNwsPjmm2900003qX379jrttNN03nnnadOmTcGo7djcbmnYMGngQKm42H9dcbFv+bBhhAsAABpQRCCNDxw4oP79+2vQoEF65513FB8fr927d6tt27ZBKu8Yysqk0lKpoEDKyJBycyWXyxcqMjJ8y6vbOZ0NXx8AAC1QQMHiySeflMvlUlZWVs2yzp07267pxCQl+cJEdYjIyJAWL5bGjvXNp6b61iclhaY+AABaoIAuhbz11lvq27evrrvuOsXHx6tPnz5auHDhMbfxer3yeDx+kzUuly88pKb6wkT//v6hwuWyty8AAHBcAQWLgoICzZ8/X127dtXKlSt1xx136J577tErr7xS7zaZmZlyOp01k8v2h73L5eupONLixYQKAABCwGGMMSfaOCoqSn379tX69etrlt1zzz3Ky8vTxx9/XOc2Xq9XXq+3Zt7j8cjlcsntdisuLu4USv+no8dUSPRYAABgmcfjkdPpPO7nd0A9FgkJCerZs6ffsh49eqioqKjebaKjoxUXF+c3WXNkqEhNlT766NfLIhkZtb8tAgAAgiqgYNG/f3/t2LHDb9nOnTvVqVMnq0WdkJIS/1CRmyv16+c/5iIjo/77XAAAAOsCChZTp07Vh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syePVvbtm3T8OHD9f7776tnz55atmxZnfvq2LGjysvLdeDAAb/lpaWlOvPMMwM8UgAAji+gYPHdd99p7Nix6tatmwYPHqxPPvlEK1as0GWXXRas+gJXXCyNHeu/bOzY2gM6LenatatiYmK0evXqE2q/du1a9evXT3feeaf69OmjLl261Nnr0KdPH82cOVPr169X79699dprr9WsS0tL09SpU7Vq1SqNGjVKWVlZde7rggsuUGRkpN59992aZfv27dMXX3yhfv36BXikAAAcX0CDN1966aVg1WHHkQM1U1N9PRVjx9Ye0GlRq1atNGPGDN13332KiopS//799f3332vbtm265ZZbarXv0qWLXnnlFa1cuVIpKSlavHix8vLylJKSIkkqLCzUggULdNVVVykxMVE7duzQzp07dfPNN+vw4cOaPn26rr32WqWkpKikpER5eXm65ppr6qzN6XTqlltu0b//+7+rffv2ateune69916dffbZGjJkiNXzAACA1MiebnpKSkr8Q0V1iMjN/XV5Roa0Zo31xz0//PDDioiI0B//+Ed9++23SkhI0B133FFn2zvuuEP5+fn6wx/+IIfDoeuvv1533nlnza3RTzvtNH311VdatGiRfvjhByUkJOiuu+7Sv/7rv6qiokI//PCDbr75Zn333Xc644wzNGrUKD3yyCP11vbcc88pIiJCo0eP1uHDhzV48GC9/PLLPHUUABAUDmOMacgdejweOZ1Oud1uxcXF+a37+eefVVhYqJSUFLVq1SqwN66+j0Vpae2eieqejPh4acUK6183hb9T+j0CABqlY31+H6n59Fg4nb7QUFZWu0fC5fL1VMTGEioAAAii5hMsJF9oqC84WL78AQAAajvl+1gAAABUI1gAAABrCBYAAMAaggUAALCGYAEAAKwhWAAAAGsIFgAAwBqCRZDt2bNHDodD+fn5oS4FAICgI1g0cwsWLFBGRobi4uLkcDh08ODBUJcEAAgSY6S8PN/PUGm2waIxnNzG4KefftKwYcP0wAMPhLoUAECQZWdLF10kvfpq6GpotsGiIU9uVVWVnnzySXXp0kXR0dFKTk7WnDlz6mxbWVmpW265RSkpKYqJiVG3bt00b948vza5ubm66KKL1Lp1a7Vt21b9+/fX3r17JUlbtmzRoEGDFBsbq7i4OF1wwQXauHFjvbVNmTJF999/vy6++GJ7BwwAaHQqKqRZs3yvZ83yzYdC83pWyD8dfXLHjJEignikM2fO1MKFC/Xcc89pwIAB2rdvn7766qs621ZVVSkpKUlLly7VGWecofXr1+v2229XQkKCRo8erYqKCl199dW67bbbtGTJEpWXl+vTTz+Vw+GQJN14443q06eP5s+fr/DwcOXn5ysyMjJ4BwcAaBKWLJEKC32vCwqknBzpppsavo5mGSwa8uSWlZVp3rx5euGFFzRu3DhJ0llnnaUBAwbU2T4yMlKPPPJIzXxKSorWr1+vpUuXavTo0fJ4PHK73bryyit11llnSZJ69OhR076oqEjTp09X9+7dJUldu3YNzoEBAJqM6j+oHQ7fEICwsIb5w7ouze5SyJEnV/r15AarS2j79u3yer0aPHjwCW/z4osvqm/fvurQoYPatGmjhQsXqqioSJLUrl07jR8/XpdffrlGjBihefPmad++fTXbTps2TbfeequGDBmiuXPnavfu3daPCQDQtFT/QV09rrCq6tc/rBtaswsWDX1yY2JiAmq/dOlSTZ06VRMnTtSqVauUn5+vCRMmqLy8vKZNVlaWPv74Y/Xr10+vv/660tLStGHDBknS7NmztW3bNg0fPlzvv/++evbsqWXLllk9JgBA03H0H9TVgv2HdX2aVbAIxcnt2rWrYmJitHr16hNqv3btWvXr10933nmn+vTpoy5dutTZ69CnTx/NnDlT69evV+/evfXaa6/VrEtLS9PUqVO1atUqjRo1SllZWdaOBwDQtBz9B3W1UPVaNKtgEYqT26pVK82YMUP33XefXnnlFe3evVsbNmzQSy+9VGf7Ll26aOPGjVq5cqV27typhx9+WHl5eTXrCwsLNXPmTH388cfau3evVq1apZ07d6pHjx46fPiw7rrrLuXm5mrv3r366KOPlJeX5zcG42j79+9Xfn6+du3aJUnaunWr8vPz9Y9//MPuiQAANLj6/qCuFpJeC9PA3G63kWTcbnetdYcPHzZffvmlOXz4cMDv+8svxqSkGONwGOOLFv5TWJgxqam+drZVVlaaxx9/3HTq1MlERkaa5ORk88QTTxhjjCksLDSSzObNm40xxvz8889m/Pjxxul0mrZt25p/+7d/M/fff78599xzjTHG7N+/31x99dUmISHBREVFmU6dOpk//vGPprKy0ni9XjNmzBjjcrlMVFSUSUxMNHfdddcxz9esWbOMpFpTVlaW/RPxT6fyewQAnLgPPqj7M+/o6YMPTn1fx/r8PpLDmIa9hZTH45HT6ZTb7VZcXJzfup9//lmFhYVKSUlRq1atAnrf3Fxp0KDjt/vgAykjI6C3RoBO5fcIADhxXq/01lu+n/WJjpauusr381Qc6/P7SM3m66bp6dLSpcc/uenpDVcTAADBFB0tXXddqKvw12yCRWM8uQAAtDTNavAmAAAILYIFAACwhmABAACsIVgAAABrCBYAAMAaggUAALCGYAEAAKwhWDRjGRkZmjJlSqjLAAC0IAQL1MjNzZXD4dDBgwdDXQoAoIlqNnfePFJlVaXWFq3VvrJ9SohN0CXJlyg8LDzUZQEA0Ow1ux6LN7e/qc7zOmvQokG64c0bNGjRIHWe11lvbn8zaPvMyMjQPffco/vuu0/t2rVTx44dNXv2bL82brdbt99+u+Lj4xUXF6ff/e532rJlS8268PBwbdq0SZJkjFG7du104YUX1my/ZMkSJSQk1FvDjz/+qJtvvllt2rRRQkKCnnnmmVptsrOz1bdvX8XGxqpjx4664YYbVFpaKknas2ePBv3zKW6nn366HA6Hxo8fL0lasWKFBgwYoLZt26p9+/a68sortXv37pM+XwCA5qtZBYs3t7+pa5deqxJPid/ybzzf6Nql1wY1XCxatEitW7fWJ598oqeeekqPPvqo3n33XUm+oDB8+HDt379ff/vb37Rp0yadf/75Gjx4sP7xj3/I6XTqvPPOU25uriTp888/r/np8Xgk+S5TDBw4sN79T58+XR988IGWLVumVatWKTc3tyaoVCsvL9djjz2mLVu2aPny5SosLKwJDy6XS2+88YYkaceOHdq3b5/mzZsnyRdapk2bpry8PK1evVphYWH6l3/5F1VVVVk7f82a2y2VlNS9rqTEtx4AmotAnsX+xBNPmL59+5o2bdqYDh06mJEjR5qvvvrK2vPcDx8+bL788ktz+PDhgN7TGG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GjXK3HTTTSGqKDh++uknEx4ebt5++22/5eeee6558MEHG7wexliEgNvtliS1a9cuxJUET2VlpXJycvTjjz8qPT091OUEzaRJkzR8+HANGTIk1KUEzddff63ExESlpKRozJgxKigoCHVJQfHWW2+pb9++uu666xQfH68+ffpo4cKFoS4r6MrLy5Wdna2JEyfWPBiyuRgwYIBWr16tnTt3SpK2bNmidevW6YorrghxZXZVVFSosrJSrVq18lseExOjdevWNXg9Ib0U0hIZYzRt2jQNGDBAvXv3DnU51m3dulXp6en6+eef1aZNGy1btkw9e/YMdVlBkZOTo88++yxk1zEbwm9/+1u98sorSktL03fffafHH39c/fr107Zt29S+fftQl2dVQUGB5s+fr2nTpumBBx7Qp59+qnvuuUfR0dG6+eabQ11e0CxfvlwHDx7U+PHjQ12KdTNmzJDb7Vb37t0VHh6uyspKzZkzR9dff32oS7MqNjZW6enpeuyxx9SjRw+deeaZWrJkiT755BN17dq14Qtq8D6SeqiFXAq58847TadOnUxxcXGoSwkKr9drvv76a5OXl2fuv/9+c8YZZ5ht27aFuizrioqKTHx8vMnPz69Z1hwvhRzt0KFD5swzzzTPPPNMqEuxLjIy0qSnp/stu/vuu83FF18coooaxtChQ82VV14Z6jKCYsmSJSYpKcksWbLEfP755+aVV14x7dq1My+//HKoS7Nu165d5tJLLzWSTHh4uLnwwgvNjTfeaHr06NHgtRAsGtBdd91lkpKSTEFBQahLaTCDBw82t99+e6jLsG7ZsmU1/wNXT5KMw+Ew4eHhpqKiItQlBs2QIUPMHXfcEeoyrEtOTja33HKL37L//u//NomJiSGqKPj27NljwsLCzPLly0NdSlAkJSWZF154wW/ZY489Zrp16xaiioLv0KFD5ttvvzXGGDN69GhzxRVXNHgNXAppAMYY3X333Vq2bJlyc3OVkpIS6pIajDFGXq831GVYN3jwYG3dutVv2YQJE9S9e3fNmDGj2X5zwuv1avv27brkkktCXYp1/fv3r/U18J07d6pTp04hqij4srKyFB8fr+HDh4e6lKD46aefFBbmP5QwPDy8WX7dtFrr1q3VunVrHThwQCtXrtRTTz3V4DWENFgcOnRIu3btqpkvLCxUfn6+2rVrp+Tk5BBWZtekSZP02muv6c9//rNiY2O1f/9+SZLT6VRMTEyIq7PngQce0O9//3u5XC6VlZUpJydHubm5WrFiRahLsy42NrbWGJnWrVurffv2zWrszL333qsRI0YoOTlZpaWlevzxx+XxeDRu3LhQl2bd1KlT1a9fPz3xxBMaPXq0Pv30Uy1YsEALFiwIdWlBUVVVpaysLI0bN04REc3zb8wRI0Zozpw5Sk5OVq9evbR582Y9++yzmjhxYqhLs27lypUyxqhbt27atWuXpk+frm7dumnChAkNX0yD95Ec4YMPPjCSak3jxo0LZVnW1XWMkkxWVlaoS7Nq4sSJplOnTiYqKsp06NDBDB482KxatSrUZTWY5jjG4g9/+INJSEgwkZGRJjEx0YwaNapZjpmp9pe//MX07t3bREdHm+7du5sFCxaEuqSgWblypZFkduzYEepSgsbj8ZjJkyeb5ORk06pVK5OammoefPBB4/V6Q12ada+//rpJTU01UVFRpmPHjmbSpEnm4MGDIamFx6YDAABruI8FAACwhmABAACsIVgAAABrCBYAAMAaggUAALCGYAEAAKwhWAAAAGsIFgAAwBqCBQAAsIZgAQAArCFYAAAAawgWAADAmv8Hb4pTd+blHD0AAAAASUVORK5CYII=",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.scatter(trainX[trainY==0,0] ,trainX[trainY==0,1] ,color='red'  ,marker='x'  ,label='class 0') # 取出trainX中分类为0 第0列为x轴坐标，第1列为y轴坐标\n",
    "plt.scatter(trainX[trainY==1,0] ,trainX[trainY==1,1] ,color='blue' ,marker='^'  ,label='class 1') # 取出trainX中分类为1 第0列为x轴坐标，第1列为y轴坐标\n",
    "dataNew=np.array([4,5]) #创建一个[4,5] 的新数据\n",
    "plt.scatter(dataNew[0],dataNew[1],color='green',marker='o',label='new data') # 画出新数据点\n",
    "\n",
    "plt.legend()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "预测data_new的类别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[np.float64(2.879236009777594),\n",
       " np.float64(0.5),\n",
       " np.float64(3.006659275674582),\n",
       " np.float64(1.9723082923316022),\n",
       " np.float64(4.47213595499958),\n",
       " np.float64(2.692582403567252),\n",
       " np.float64(3.3526109228480423),\n",
       " np.float64(5.080354318352215),\n",
       " np.float64(5.5901699437494745)]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "distanceArr = [np.sqrt(np.sum((item-dataNew)**2)) for item in dataX]  #遍历dataX中的每一个数据，计算与dataNew的距离(该计算公式与sklearn中的KNN算法相同)\n",
    "distanceArr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 3, 5, 0, 2, 6, 4, 7, 8])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sortIndex = np.argsort(distanceArr, axis=0) # 按行排序 得到排序后索引数组\n",
    "sortIndex\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[np.int64(0), np.int64(0), np.int64(1), np.int64(0), np.int64(0)]"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "k=5 # 选择最近邻的数目\n",
    "karr=[trainY[i] for i in sortIndex[:k]] #从trainY中选取举例最近的前k个分类标记\n",
    "karr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n"
     ]
    }
   ],
   "source": [
    "from collections import Counter\n",
    "restuple=Counter(karr).most_common(1)[0]\n",
    "print(restuple[0])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "KNN算法实现"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[4, 5]])"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.neighbors import KNeighborsClassifier\n",
    "\n",
    "knn=KNeighborsClassifier(n_neighbors=5)\n",
    "knn.fit(trainX,trainY)\n",
    "knn.predict(dataNew.reshape(1,-1))\n",
    "dataNew.reshape(1,-1)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "machineLearn",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
